package servlet;

import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.*;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;

//@WebServlet("/test")
public class TestServlet extends HttpServlet {

    // alt +insert
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("doGet");
        // 设置响应头
        resp.setCharacterEncoding("utf-8");
        resp.setContentType("text/html");
// 通过输出流响应网页数据
        PrintWriter out = resp.getWriter();
        out.println("<h2>");
        out.println(req.getParameter("username"));
        out.println("</h2>");
        out.flush();
        out.close();
        super.doGet(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("doPost");

        super.doPost(req, resp);
    }

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        System.out.println("service");
//        super.service(req, resp);
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        System.out.println(username);
        System.out.println(password);
        String string = req.getRequestURL().toString();
        System.out.println(string);
        String header = req.getHeader("User-Agent");
        System.out.println(header);

        resp.setContentType("text/html");
        resp.setHeader("name1", "value1");

        // 转发
//        req.setAttribute("key2", "value2");
//        req.getRequestDispatcher("WEB-INF/bootstrap.html").forward(req,resp);

        // 重定向
//        resp.sendRedirect("servletB?key2=value2");
//        resp.sendRedirect("pages/bootstrap.html");
        //
        Cookie cookie = new Cookie("cookie1", "value1");
        Cookie cookie1 = new Cookie("cookie2", "value2");
//        cookie.setMaxAge(60);
        resp.addCookie(cookie);
        resp.addCookie(cookie1);

        Cookie[] cookies = req.getCookies();
        for (Cookie cookie2 : cookies) {
            System.out.println(cookie2.getName());
            System.out.println(cookie2.getValue());
        }

        HttpSession session = req.getSession();
        session.setAttribute("username", username);
        session.setAttribute("username", "lisi");
        String username1 = (String) session.getAttribute("username");
        System.out.println(username1);


        ServletContext servletContext = getServletContext();
        String contextPath = servletContext.getContextPath();
        System.out.println(contextPath);
        String realPath = servletContext.getRealPath("/file");
        System.out.println(realPath);


        String key1 = servletContext.getInitParameter("key1");
        System.out.println(key1);
        Enumeration<String> initParameterNames = servletContext.getInitParameterNames();

        while (initParameterNames.hasMoreElements()) {
            String nextElement = initParameterNames.nextElement();
            String initParameter = servletContext.getInitParameter(nextElement);
            System.out.println(nextElement +" "+initParameter);
        }

        ServletConfig servletConfig = getServletConfig();

        String servet1 = servletConfig.getInitParameter("servet1");
        System.out.println(servet1);
        Enumeration<String> initParameterNames1 = servletConfig.getInitParameterNames();
        while (initParameterNames1.hasMoreElements()) {
            String s = initParameterNames1.nextElement();
            String initParameter = servletConfig.getInitParameter(s);
            System.out.println(s+" "+ initParameter);
        }
    }

    @Override
    public void destroy() {
        System.out.println("destroy");
        super.destroy();
    }

    @Override
    public void init() throws ServletException {
        System.out.println("init");
        super.init();
    }
}
